package com.chengcang.admin.modules.busi.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chengcang.admin.common.annotation.LogOperation;
import com.chengcang.admin.common.utils.ExcelUtils;
import com.chengcang.admin.modules.busi.dto.BusiOrderDTO;
import com.chengcang.admin.modules.busi.dto.BusiOrderInfoDTO;
import com.chengcang.admin.modules.busi.excel.BusiOrderExcel;
import com.chengcang.admin.modules.busi.service.BusiOrderService;
import com.chengcang.common.constant.Constant;
import com.chengcang.common.utils.Result;
import com.chengcang.common.validator.AssertUtils;
import com.chengcang.common.validator.ValidatorUtils;
import com.chengcang.common.validator.group.AddGroup;
import com.chengcang.common.validator.group.DefaultGroup;
import com.chengcang.common.validator.group.UpdateGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
 *
 *
 * @author zhanli
 * @since 1.0.0 2023-01-10
 */
@RestController
@RequestMapping("busi/order")
@Api(tags="订单模块")
public class BusiOrderController {
    @Autowired
    private BusiOrderService busiOrderService;

    @GetMapping("/demand/page")
    @ApiOperation("订单分页")
    @ApiImplicitParams({
        @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
        @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
    @RequiresPermissions("busi:order:page")
    public Result<Page<BusiOrderDTO>> demandPage(@ApiIgnore @RequestParam Map<String, Object> params){
        return new Result<Page<BusiOrderDTO>>().ok(busiOrderService.getDemandPage(params));
    }

    @GetMapping("/supply/page")
    @ApiOperation("订单履约分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
            @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
//    @RequiresPermissions("busi:order:appoint")
    public Result<Page<BusiOrderDTO>> supplypage(@ApiIgnore @RequestParam Map<String, Object> params){
        return new Result<Page<BusiOrderDTO>>().ok(busiOrderService.getSupplyPage(params));
    }

    @GetMapping("/all/page")
    @ApiOperation("订单分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
            @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
    public Result<Page<BusiOrderDTO>> allPage(@ApiIgnore @RequestParam Map<String, Object> params){
        return new Result<Page<BusiOrderDTO>>().ok(busiOrderService.getAllPage(params));
    }


    @GetMapping("{id}")
    @ApiOperation("信息")
    @RequiresPermissions("busi:order:info")
    public Result<BusiOrderInfoDTO> get(@PathVariable("id") Long id){
        BusiOrderInfoDTO data = busiOrderService.detail(id);
        return new Result<BusiOrderInfoDTO>().ok(data);
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("保存")
    @RequiresPermissions("busi:order:save")
    public Result<?> save(@RequestBody @Validated BusiOrderDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
        busiOrderService.saveOrder(dto);
        return new Result<Object>();
    }

    @PutMapping
    @ApiOperation("发货")
    @LogOperation("发货")
    @RequiresPermissions("busi:order:deliver")
    public Result<?> deliver(@RequestBody BusiOrderDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
        busiOrderService.deliver(dto);
        return new Result<Object>();
    }

    @ApiOperation("批量关单")
    @LogOperation("批量关单")
    @PutMapping("close")
    public Result<?> close(@RequestBody List<BusiOrderDTO> dtos){
        busiOrderService.close(dtos);
        return new Result<Object>();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("删除")
    @RequiresPermissions("busi:order:delete")
    public Result<?> delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");
        busiOrderService.delete(ids);
        return new Result<Object>();
    }

    @GetMapping("export")
    @ApiOperation("导出")
    @LogOperation("导出")
    @RequiresPermissions("busi:order:export")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<BusiOrderDTO> list = busiOrderService.list(params);
        ExcelUtils.exportExcelToTarget(response, null, list, BusiOrderExcel.class);
    }

}
